"use strict";
const Mogo = require("./index");

// window.Mogo = Mogo;

// MG.request({
//   url: "/apis/mogo-ql/class/plan",
//   method: "post"
// }).catch(err => {
//   console.error(err);
// });
Mogo.init({
  domain: "http://comment.mogofun.cn",
  ajaxErrorHandel: function(error) {
    console.warn(error);
  },
  upload_url: "http://comment.mogofun.cn/upload"
});
document.getElementById("login").addEventListener("click", function() {
  console.log("im click");
  Mogo.User.login({
    username: "ArH",
    password: "you password"
  });
});
document.getElementById("logout").addEventListener("click", function() {
  Mogo.User.logout();
});

document.getElementById("check").addEventListener("click", function() {
  Mogo.User.checkUserLogin();
});

document.getElementById("upload").addEventListener("click", function() {
  let file = document.getElementById("file").files[0];
  Mogo.upload(file, function(progress) {
    console.log(progress);
  }).then(res => {
    console.log(res);
  });
});

document.getElementById("queryTest").addEventListener("click", function() {
  let plan = new Mogo.Model("plan");
  plan
    .where({
      title: ["like", "刘"]
    })
    .order("id asc")
    .with(["owner", "comment.user", "joiner", "tags"])
    .attr(["duration"])
    .find()
    .then(res => {
      console.log(res);
    });
});

document.getElementById("get").addEventListener("click", function() {
  let model = document.getElementById("model").value;
  let method = document.getElementById("method").value;
  let Model = new Mogo.Model(model);
  // plan.find();
  // Model.find().then(res => {
  //   console.log(res);
  // });
  Model.execute(method);
});
